package com.gszy.auth.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcUtils {

    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();

    static {
        try {
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/webdemo");
            dataSource.setUser("root");
            dataSource.setPassword("123456");

            dataSource.setInitialPoolSize(10);
            dataSource.setMaxPoolSize(20);
            dataSource.setMinPoolSize(10);
            dataSource.setAcquireIncrement(3);
        }catch (PropertyVetoException e){
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException{
        return dataSource.getConnection();
    }


//    static {
//        try {
//            Class.forName("com.mysql.jdbc.Driver");
//        }catch (ClassNotFoundException e){
//            e.printStackTrace();
//        }
//    }
//
//    public static Connection getConnection(){
//        return getConnection(
//                "jdbc:mysql://localhost:3306/webdemo",
//                "root",
//                "123456"
//        );
//    }
//
//    public static Connection getConnection(String url,String username,String password) {
//        Connection connection=null;
//        try{
//            connection= DriverManager.getConnection(url, username, password);
//        }catch (SQLException throwables){
//            throwables.printStackTrace();
//        }
//        return connection;
//    }

    public static void closeAll(ResultSet resultSet , Statement statement,Connection connection){

        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
    }
}